#include <commands/client/c_request_model_description.h>
#include <commands/server/s_send_model_description.h>

#include <app\states\connectionState.h>
#include <app\states\modelInfoState.h>

#include <sstream>

using std::stringstream;
using std::endl;

REGISTER_COMMAND(CLIENT_REQUEST_MODEL_DESCRIPTION, c_request_model_description)

c_request_model_description::c_request_model_description() : command(0, NULL) {
}

c_request_model_description::c_request_model_description(unsigned int _type_id, streamingConnection *_connection) : command(_type_id, _connection) {
}

void
c_request_model_description::decode(stringstream &bytes_stream, int size) {
#ifdef SERVER

  #ifdef DEBUG_INFO
  std::cerr << "SERVER DECODE: c_request_model_description" << std::endl;
  #endif

  // determine directory
  streamingConnection *conn = getconnection();
  connectionState *cstate = GET_STATE(connectionState, conn);

  s_send_model_description *cmd = static_cast<s_send_model_description *>(commandFactory::instance().createCommand(SERVER_SEND_MODEL_DESCRIPTION, conn));

  modelInfoState *mfstate = GET_STATE(modelInfoState, conn);
  int size_;
  unsigned char *data = mfstate->getRawData(size_);

  cmd->setData(data, size_);
  cmd->execute();

  delete cmd;

#endif
}

void 
c_request_model_description::execute() {
#ifdef CLIENT

  #ifdef DEBUG_INFO
  std::cerr << "CLIENT EXECUTE: c_request_model_description" << std::endl;
  #endif

  unsigned int *id = new unsigned int[1];
  unsigned int *total_size = new unsigned int[1];

  total_size[0] = sizeof(unsigned int); // total size itself

  id[0] = getTypeId();
  total_size[0] += sizeof(unsigned int); // id size

  sendBytes(total_size, sizeof(unsigned int));
  sendBytes(id, sizeof(unsigned int));

#endif
}
